﻿namespace UnityEditor
{
    using System;
    using UnityEditorInternal;
    using UnityEngine;

    [CustomEditor(typeof(Light)), CanEditMultipleObjects]
    internal class LightEditor : Editor
    {
        internal static Color kGizmoDisabledLight = new Color(0.5294118f, 0.454902f, 0.1960784f, 0.5019608f);
        internal static Color kGizmoLight = new Color(0.9960784f, 0.9921569f, 0.5333334f, 0.5019608f);
        private AnimValueManager m_Anims = new AnimValueManager();
        private SerializedProperty m_AreaSizeX;
        private SerializedProperty m_AreaSizeY;
        private SerializedProperty m_Color;
        private SerializedProperty m_Cookie;
        private SerializedProperty m_CookieSize;
        private SerializedProperty m_CullingMask;
        private SerializedProperty m_Flare;
        private SerializedProperty m_Halo;
        private SerializedProperty m_Intensity;
        private SerializedProperty m_Lightmapping;
        private SerializedProperty m_Range;
        private SerializedProperty m_RenderMode;
        private SerializedProperty m_ShadowsBias;
        private SerializedProperty m_ShadowsResolution;
        private SerializedProperty m_ShadowsSoftness;
        private SerializedProperty m_ShadowsSoftnessFade;
        private SerializedProperty m_ShadowsStrength;
        private SerializedProperty m_ShadowsType;
        private AnimBool m_ShowAreaOptions = new AnimBool();
        private AnimBool m_ShowAreaWarning = new AnimBool();
        private AnimBool m_ShowDirOptions = new AnimBool();
        private AnimBool m_ShowForwardShadowsWarning = new AnimBool();
        private AnimBool m_ShowPointOptions = new AnimBool();
        private AnimBool m_ShowShadowOptions = new AnimBool();
        private AnimBool m_ShowShadowWarning = new AnimBool();
        private AnimBool m_ShowSoftOptions = new AnimBool();
        private AnimBool m_ShowSpotOptions = new AnimBool();
        private SerializedProperty m_SpotAngle;
        private SerializedProperty m_Type;
        private bool m_UsingDeferred;

        private void InitShowOptions()
        {
            this.m_ShowSpotOptions.value = this.spotOptionsValue;
            this.m_ShowPointOptions.value = this.pointOptionsValue;
            this.m_ShowSoftOptions.value = this.softOptionsValue;
            this.m_ShowDirOptions.value = this.dirOptionsValue;
            this.m_ShowAreaOptions.value = this.areaOptionsValue;
            this.m_ShowShadowOptions.value = this.shadowOptionsValue;
            this.m_ShowShadowWarning.value = this.shadowWarningValue;
            this.m_ShowForwardShadowsWarning.value = this.forwardWarningValue;
            this.m_ShowAreaWarning.value = this.areaWarningValue;
            this.m_Anims.Add(this.m_ShowSpotOptions);
            this.m_Anims.Add(this.m_ShowPointOptions);
            this.m_Anims.Add(this.m_ShowDirOptions);
            this.m_Anims.Add(this.m_ShowAreaOptions);
            this.m_Anims.Add(this.m_ShowShadowOptions);
            this.m_Anims.Add(this.m_ShowShadowWarning);
            this.m_Anims.Add(this.m_ShowForwardShadowsWarning);
            this.m_Anims.Add(this.m_ShowSoftOptions);
            this.m_Anims.Add(this.m_ShowAreaWarning);
        }

        private void OnEnable()
        {
            this.m_Type = base.serializedObject.FindProperty("m_Type");
            this.m_Range = base.serializedObject.FindProperty("m_Range");
            this.m_SpotAngle = base.serializedObject.FindProperty("m_SpotAngle");
            this.m_CookieSize = base.serializedObject.FindProperty("m_CookieSize");
            this.m_Color = base.serializedObject.FindProperty("m_Color");
            this.m_Intensity = base.serializedObject.FindProperty("m_Intensity");
            this.m_Cookie = base.serializedObject.FindProperty("m_Cookie");
            this.m_ShadowsType = base.serializedObject.FindProperty("m_Shadows.m_Type");
            this.m_ShadowsStrength = base.serializedObject.FindProperty("m_Shadows.m_Strength");
            this.m_ShadowsResolution = base.serializedObject.FindProperty("m_Shadows.m_Resolution");
            this.m_ShadowsBias = base.serializedObject.FindProperty("m_Shadows.m_Bias");
            this.m_ShadowsSoftness = base.serializedObject.FindProperty("m_Shadows.m_Softness");
            this.m_ShadowsSoftnessFade = base.serializedObject.FindProperty("m_Shadows.m_SoftnessFade");
            this.m_Halo = base.serializedObject.FindProperty("m_DrawHalo");
            this.m_Flare = base.serializedObject.FindProperty("m_Flare");
            this.m_RenderMode = base.serializedObject.FindProperty("m_RenderMode");
            this.m_CullingMask = base.serializedObject.FindProperty("m_CullingMask");
            this.m_Lightmapping = base.serializedObject.FindProperty("m_Lightmapping");
            this.m_AreaSizeX = base.serializedObject.FindProperty("m_AreaSize.x");
            this.m_AreaSizeY = base.serializedObject.FindProperty("m_AreaSize.y");
            this.InitShowOptions();
            this.m_UsingDeferred = CameraUtility.DoesAnyCameraUseDeferred();
        }

        public override void OnInspectorGUI()
        {
            if (this.m_Anims.callback == null)
            {
                this.m_Anims.callback = new AnimValueManager.Callback(this.Repaint);
            }
            base.serializedObject.Update();
            this.UpdateShowOptions();
            EditorGUILayout.PropertyField(this.m_Type, new GUILayoutOption[0]);
            float num = ((!this.m_ShowDirOptions.wantsUpdate || !this.m_ShowAreaOptions.wantsUpdate) || (!this.m_ShowDirOptions.target && !this.m_ShowAreaOptions.target)) ? (1f - Mathf.Max(this.m_ShowDirOptions.faded, this.m_ShowAreaOptions.faded)) : 0f;
            if (EditorGUILayout.BeginFadeGroup(this.m_ShowAreaWarning.faded))
            {
                EditorGUILayout.HelpBox(EditorGUIUtility.TextContent("LightEditor.AreaLightsProOnly").text, MessageType.Warning, false);
            }
            EditorGUILayout.EndFadeGroup();
            if (EditorGUILayout.BeginFadeGroup(num))
            {
                EditorGUILayout.PropertyField(this.m_Range, new GUILayoutOption[0]);
            }
            EditorGUILayout.EndFadeGroup();
            if (EditorGUILayout.BeginFadeGroup(this.m_ShowSpotOptions.faded))
            {
                EditorGUILayout.Slider(this.m_SpotAngle, 1f, 179f, new GUILayoutOption[0]);
            }
            EditorGUILayout.EndFadeGroup();
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.m_Color, new GUILayoutOption[0]);
            EditorGUILayout.Slider(this.m_Intensity, 0f, 8f, new GUILayoutOption[0]);
            if (EditorGUILayout.BeginFadeGroup(1f - this.m_ShowAreaOptions.faded))
            {
                EditorGUILayout.PropertyField(this.m_Cookie, new GUILayoutOption[0]);
                if (EditorGUILayout.BeginFadeGroup(this.m_ShowDirOptions.faded))
                {
                    EditorGUILayout.PropertyField(this.m_CookieSize, new GUILayoutOption[0]);
                }
                EditorGUILayout.EndFadeGroup();
                EditorGUILayout.Space();
                EditorGUILayout.PropertyField(this.m_ShadowsType, new GUIContent("Shadow Type", "Shadow cast options"), new GUILayoutOption[0]);
                if (EditorGUILayout.BeginFadeGroup(this.m_ShowShadowOptions.faded))
                {
                    EditorGUI.indentLevel++;
                    if (EditorGUILayout.BeginFadeGroup(this.m_ShowForwardShadowsWarning.faded))
                    {
                        EditorGUILayout.HelpBox(EditorGUIUtility.TextContent("LightEditor.ForwardRenderingShadowsWarning").text, MessageType.Warning, false);
                    }
                    EditorGUILayout.EndFadeGroup();
                    if (EditorGUILayout.BeginFadeGroup(this.m_ShowShadowWarning.faded))
                    {
                        EditorGUILayout.HelpBox(EditorGUIUtility.TextContent("LightEditor.NoShadowsWarning").text, MessageType.Warning, false);
                    }
                    EditorGUILayout.EndFadeGroup();
                    if (EditorGUILayout.BeginFadeGroup(1f - this.m_ShowShadowWarning.faded))
                    {
                        EditorGUILayout.Slider(this.m_ShadowsStrength, 0f, 1f, new GUILayoutOption[0]);
                        EditorGUILayout.PropertyField(this.m_ShadowsResolution, new GUILayoutOption[0]);
                        EditorGUILayout.Slider(this.m_ShadowsBias, 0f, 0.5f, new GUILayoutOption[0]);
                        if (EditorGUILayout.BeginFadeGroup(this.m_ShowSoftOptions.faded))
                        {
                            EditorGUILayout.Slider(this.m_ShadowsSoftness, 1f, 8f, new GUILayoutOption[0]);
                            EditorGUILayout.Slider(this.m_ShadowsSoftnessFade, 0.1f, 5f, new GUILayoutOption[0]);
                        }
                        EditorGUILayout.EndFadeGroup();
                    }
                    EditorGUILayout.EndFadeGroup();
                    EditorGUI.indentLevel--;
                }
                EditorGUILayout.EndFadeGroup();
                EditorGUILayout.Space();
                EditorGUILayout.PropertyField(this.m_Halo, new GUILayoutOption[0]);
                EditorGUILayout.PropertyField(this.m_Flare, new GUILayoutOption[0]);
                EditorGUILayout.PropertyField(this.m_RenderMode, new GUILayoutOption[0]);
                EditorGUILayout.PropertyField(this.m_CullingMask, new GUILayoutOption[0]);
                EditorGUILayout.PropertyField(this.m_Lightmapping, new GUILayoutOption[0]);
            }
            EditorGUILayout.EndFadeGroup();
            if (EditorGUILayout.BeginFadeGroup(this.m_ShowAreaOptions.faded))
            {
                EditorGUILayout.PropertyField(this.m_AreaSizeX, new GUIContent("Width"), new GUILayoutOption[0]);
                EditorGUILayout.PropertyField(this.m_AreaSizeY, new GUIContent("Height"), new GUILayoutOption[0]);
            }
            EditorGUILayout.EndFadeGroup();
            EditorGUILayout.Space();
            base.serializedObject.ApplyModifiedProperties();
        }

        private void OnSceneGUI()
        {
            Light target = (Light) base.target;
            Color color = Handles.color;
            if (target.enabled)
            {
                Handles.color = kGizmoLight;
            }
            else
            {
                Handles.color = kGizmoDisabledLight;
            }
            float range = target.range;
            switch (target.type)
            {
                case LightType.Spot:
                {
                    Color color2 = Handles.color;
                    color2.a = Mathf.Clamp01(color.a * 2f);
                    Handles.color = color2;
                    Undo.SetSnapshotTarget(target, "Adjust Spot Light");
                    Vector2 angleAndRange = new Vector2(target.spotAngle, target.range);
                    angleAndRange = Handles.ConeHandle(target.transform.rotation, target.transform.position, angleAndRange, 1f, 1f, true);
                    if (GUI.changed)
                    {
                        target.spotAngle = angleAndRange.x;
                        target.range = Mathf.Max(angleAndRange.y, 0.01f);
                    }
                    break;
                }
                case LightType.Point:
                    Undo.SetSnapshotTarget(target, "Adjust Point Light");
                    range = Handles.RadiusHandle(Quaternion.identity, target.transform.position, range, true);
                    if (GUI.changed)
                    {
                        target.range = range;
                    }
                    break;

                case LightType.Area:
                {
                    Undo.SetSnapshotTarget(target, "Adjust Area Light");
                    EditorGUI.BeginChangeCheck();
                    Vector2 vector2 = Handles.DoRectHandles(target.transform.rotation, target.transform.position, target.areaSize);
                    if (EditorGUI.EndChangeCheck())
                    {
                        target.areaSize = vector2;
                    }
                    break;
                }
            }
            Handles.color = color;
        }

        private void UpdateShowOptions()
        {
            this.m_ShowSpotOptions.target = this.spotOptionsValue;
            this.m_ShowPointOptions.target = this.pointOptionsValue;
            this.m_ShowSoftOptions.target = this.softOptionsValue;
            this.m_ShowDirOptions.target = this.dirOptionsValue;
            this.m_ShowAreaOptions.target = this.areaOptionsValue;
            this.m_ShowShadowOptions.target = this.shadowOptionsValue;
            this.m_ShowShadowWarning.target = this.shadowWarningValue;
            this.m_ShowForwardShadowsWarning.target = this.forwardWarningValue;
            this.m_ShowAreaWarning.target = this.areaWarningValue;
        }

        private bool areaOptionsValue
        {
            get
            {
                return (this.typeIsSame && (this.light.type == LightType.Area));
            }
        }

        private bool areaWarningValue
        {
            get
            {
                return ((this.typeIsSame && !InternalEditorUtility.HasPro()) && (this.light.type == LightType.Area));
            }
        }

        private bool dirOptionsValue
        {
            get
            {
                return (this.typeIsSame && (this.light.type == LightType.Directional));
            }
        }

        private bool forwardWarningValue
        {
            get
            {
                return ((this.typeIsSame && !this.m_UsingDeferred) && (this.light.type != LightType.Directional));
            }
        }

        private Light light
        {
            get
            {
                return (base.target as Light);
            }
        }

        private bool pointOptionsValue
        {
            get
            {
                return (this.typeIsSame && (this.light.type == LightType.Point));
            }
        }

        private bool shadowOptionsValue
        {
            get
            {
                return (this.shadowTypeIsSame && (this.light.shadows != LightShadows.None));
            }
        }

        private bool shadowTypeIsSame
        {
            get
            {
                return !this.m_ShadowsType.hasMultipleDifferentValues;
            }
        }

        private bool shadowWarningValue
        {
            get
            {
                return !InternalEditorUtility.HasPro();
            }
        }

        private bool softOptionsValue
        {
            get
            {
                return (((this.shadowTypeIsSame && this.typeIsSame) && (this.light.shadows == LightShadows.Soft)) && (this.light.type == LightType.Directional));
            }
        }

        private bool spotOptionsValue
        {
            get
            {
                return (this.typeIsSame && (this.light.type == LightType.Spot));
            }
        }

        private bool typeIsSame
        {
            get
            {
                return !this.m_Type.hasMultipleDifferentValues;
            }
        }
    }
}

